﻿@* @inject IDialogService DialogService
@inject IMaterialService MaterialService
@inject IStandardBomService StandardBomService

<MudStack>
    <MudToolBar>
        <MudSpacer></MudSpacer>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle"
                   Size="Size.Small" Class="mr-5">
            保存
        </MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle"
                   Size="Size.Small">
            取消
        </MudButton>
    </MudToolBar>
    <MudForm @ref="_editRequestForm" Model="_editRequestDto">
        <MudPaper Class="pa-4" Elevation="0">
            <MudText Color="Color.Primary" Typo="Typo.h6">基本信息</MudText>
            <MudGrid>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Code" For="@(() => _editRequestDto.Code)" Label="BOM编码"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Name" For="@(() => _editRequestDto.Name)" Label="BOM名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Version" For="@(() => _editRequestDto.Version)" Label="版本号"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Contract" For="@(() => _editRequestDto.Contract)" Label="合同号"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)" Label="数据源描述"
                                  Lines="3" Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudPaper>
        <MudPaper Class="pa-4" Elevation="0">
            <MudText Color="Color.Primary" Typo="Typo.h6">产品信息</MudText>
            <MudGrid>
                <MudItem xs="6">
                    <MudAutocomplete T="MaterialInfoResponseDto"
                                     Label="物料编码" @bind-Value="_editRequestDto.Material"
                                     SearchFunc="@MaterialSearch" ToStringFunc="@(e => e?.Code)"
                                     Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined"/>

                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Name" Label="物料名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Specification" Label="规格"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.Material.Unit" Label="单位"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudNumericField @bind-Value="_editRequestDto.Quality" For="@(() => _editRequestDto.Quality)" Label="数量"
                                     Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudNumericField>
                </MudItem>
            </MudGrid>
        </MudPaper>
        <MudPaper Class="pa-4" Elevation="0">
            <MudText Color="Color.Primary" Typo="Typo.h6">明细信息</MudText>
            <MudTable T="StandardBomDetailEditRequestDto" Items="_editRequestDto.Items"
                      Hover="true" Dense="true" Breakpoint="Breakpoint.Xs" Elevation="0"
                      ReadOnly="true" FixedHeader="true" CustomFooter="true">
                <ToolBarContent>
                    <MudButton StartIcon="mdi mdi-plus" Variant="Variant.Filled" Color="Color.Primary"
                               Size="Size.Small" Class="app-default-button"
                               OnClick="AddHandler">
                        新增
                    </MudButton>
                </ToolBarContent>
                <Columns>
                    <MudColumn HeaderText="物料编号" Value="@(context.MaterialCode)"></MudColumn>
                    <MudColumn HeaderText="物料名称" Value="@(context.MaterialName)"></MudColumn>
                    <MudColumn HeaderText="规格型号" Value="@(context.Specification)"></MudColumn>
                    <MudColumn HeaderText="单位" Value="@(context.Unit)"></MudColumn>
                    <MudColumn HeaderText="材料用料" Value="@(context.Quality)"></MudColumn>
                    <MudColumn HeaderText="损耗率" Value="@(context.LossRate)"></MudColumn>
                    <MudColumn HeaderText="领料方式" Value=""></MudColumn>
                    <MudColumn HeaderText="发料仓库" Value=""></MudColumn>
                    <MudColumn HeaderText="工位" Value=""></MudColumn>
                    <MudColumn HeaderText="关键件" Value=""></MudColumn>
                    <MudColumn HeaderText="物料备注" Value=""></MudColumn>
                    <MudTemplateColumn T="StandardBomDetailEditRequestDto">
                        <Header Context="_">
                            <MudTh>操作</MudTh>
                        </Header>
                        <Row Context="_">
                            <MudTd>
                                <MudButton StartIcon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@(() => EditHandler(context))">修改</MudButton>

                                <MudButton StartIcon="@Icons.Material.Filled.Delete" Size="Size.Small" OnClick="DeleteHandler">删除</MudButton>
                            </MudTd>
                        </Row>
                    </MudTemplateColumn>
                </Columns>
            </MudTable>
        </MudPaper>
    </MudForm>
</MudStack>

@code {
    private StandardBomEditRequestDto _editRequestDto = new();

    // private StandardBomEditValidator _editValidator = new();

    private MudForm _editRequestForm = new();

    private DialogOptions _dialogOptions = new()
    {
        MaxWidth = MaxWidth.Medium,
        CloseButton = true,
        FullWidth = true,
        DisableBackdropClick = true
    };

    //private IList<MaterialInfoResponseDto> _materials = new List<MaterialInfoResponseDto>();

    [CascadingParameter]
    public StandardBomIndex? StandardBomIndex { get; set; }

    async Task AddHandler()
    {
        var dialog = await DialogService.ShowAsync<StandardBomDetailEdit>("添加物料", new DialogParameters(), _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            var itemData = (StandardBomDetailEditRequestDto)result.Data;
            _editRequestDto.Items?.Add(itemData);
        }
    }

    async Task DeleteHandler()
    {
    }

    async Task EditHandler(StandardBomDetailEditRequestDto model)
    {
        var dialogParameters = new DialogParameters();
        dialogParameters.Add("Model", model);
        var dialog = await DialogService.ShowAsync<StandardBomDetailEdit>("修改物料", dialogParameters, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            model = (StandardBomDetailEditRequestDto)result.Data;
        }
    }

    async Task SubmitHandle()
    {
        var key = StandardBomIndex?.GetKey();
        if (key > 0)
        {
            await StandardBomService.UpdateStandardBom(key, _editRequestDto);
        }
        else
        {
            await StandardBomService.CreateStandardBom(_editRequestDto);
        }
    }

    void CancelHandle()
    {
        StandardBomIndex?.SetDynamicComponentType(typeof(StandardBomTable));
    }

    private async Task<IEnumerable<MaterialInfoResponseDto>> MaterialSearch(string value)
    {
        var result = await MaterialService.QueryMaterials(new MaterialSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<MaterialInfoResponseDto>();
    }


    protected override async Task OnInitializedAsync()
    {
        _editRequestDto = new StandardBomEditRequestDto();
        var key = StandardBomIndex?.GetKey();
        if (key > 0)
        {
            var result = await StandardBomService.GetStandardBom(key.Value);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.Material.Id = result.Data.ProductId;
                _editRequestDto.Material.Code = result.Data.ProductCode;
                _editRequestDto.Material.Name = result.Data.ProductName;
                _editRequestDto.Quality = result.Data.Quality;
                _editRequestDto.Description = result.Data.Description;
                if (result.Data.Items != null)
                {
                    _editRequestDto.Items = result.Data.Items.Select(i => new StandardBomDetailEditRequestDto()
                    {
                        Id = i.Id,
                        Material = new MaterialInfoResponseDto()
                        {
                            Id = i.Id,
                            Code = i.MaterialCode,
                            Name = i.MaterialName,
                        },
                        Quality = i.Quality,
                        LossRate = i.LossRate
                    }).ToList();
                }
            }
        }
        await base.OnInitializedAsync();
    }

} *@